Imports System.Data.SqlClient

Public Class cls_config_modulo_

    Implements ICloneable

#Region "Variables ..."
    Private str_idmodulo As String = ""
    Private bit_activo As Boolean = False

    Private obj_modulo As cls_modulo_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_config_modulo_) = Nothing
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idmodulo
        End Get
    End Property
    Property idmodulo() As String
        Get
            idmodulo = str_idmodulo
        End Get
        Set(ByVal value As String)
            str_idmodulo = value
        End Set
    End Property
    Property activo() As Boolean
        Get
            activo = bit_activo
        End Get
        Set(ByVal value As Boolean)
            bit_activo = value
        End Set
    End Property

    Property modulo() As cls_modulo_
        Get
            If IsNothing(obj_modulo) Then
                obj_modulo = New cls_modulo_
                If Not obj_modulo.Buscar(idmodulo) Then
                    obj_modulo = Nothing
                End If
            End If
            modulo = obj_modulo
        End Get
        Set(ByVal value As cls_modulo_)
            obj_modulo = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_config_modulo_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_config_modulo_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_config_modulo_)
        While obj_Reader.Read()
            Dim Elemento As New cls_config_modulo_
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo"))) = False Then
                Elemento.str_idmodulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("activo"))) = False Then
                Elemento.bit_activo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("activo")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo"))) = False Then
            str_idmodulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodulo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("activo"))) = False Then
            bit_activo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("activo")))
        End If
    End Sub

    Public Function Buscar(ByVal idmodulo As String) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "config_modulo__Buscar"
                .Parameters.Add("@idmodulo", SqlDbType.NVarChar).Value = idmodulo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de config_modulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "config_modulo__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de config_modulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "config_modulo__Modificar"
                .Parameters.Add("@idmodulo", SqlDbType.NVarChar).Value = str_idmodulo
                .Parameters.Add("@activo", SqlDbType.Bit).Value = bit_activo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de config_modulo_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_config_modulo_Clon As New cls_config_modulo_
        obj_config_modulo_Clon.str_idmodulo = Me.str_idmodulo
        obj_config_modulo_Clon.bit_activo = Me.bit_activo
        obj_config_modulo_Clon.col_Items = Me.col_Items
        Return obj_config_modulo_Clon
    End Function

End Class
